package org.tokaf.bestcolumnfinder;

import org.tokaf.algorithm.Algorithm;
import org.tokaf.datasearcher.DataSearcher;
import org.tokaf.rater.Rater;

/**
 * <p>Uses both DerivationInPointFinder and DerivationInDataFinder.
 * DerivationInPointFinder is used for phaseDerivation steps,
 * DerivationInDataFinder is used for phaseDataDerivation steps.</p> <p>Copyright
 * (c) 2006</p>
 * @author Alan Eckhardt
 * @version 1.0
 */
public class SwitchingPointDerivationFinder implements BestColumnFinder {
	int phaseDerivation = 1;

	int phaseDataDerivation = 1;

	int phaseDerivationCount = 0;

	int phaseDataDerivationCount = 0;

	int phase = 0;

	DerivationInPointFinder derivation;

	DerivationInDataFinder dataDerivation;

	public SwitchingPointDerivationFinder(DataSearcher[] data, Rater rater,
			int phaseDerivation, int phaseDataDerivation) {
		this.phaseDerivation = phaseDerivation;
		this.phaseDataDerivation = phaseDataDerivation;
		derivation = new DerivationInPointFinder(data, rater);
		dataDerivation = new DerivationInDataFinder(data, rater);
	}

	public int findColumn(Algorithm alg) {
		if (phase == 0 && phaseDerivation > phaseDerivationCount) {
			phaseDerivationCount++;
			if (phaseDerivationCount == phaseDerivation) {
				phaseDerivationCount = 0;
				phase = 1;
			}
			return derivation.findColumn(alg);
		}
		if (phase == 1 && phaseDataDerivation > phaseDataDerivationCount) {
			phaseDataDerivationCount++;
			if (phaseDataDerivationCount == phaseDataDerivation) {
				phaseDataDerivationCount = 0;
				phase = 0;
			}
			return dataDerivation.findColumn(alg);
		}
		return -1;
	}
}
